import pytest
import logging
from tests.utils import htmlUtils
from time import sleep
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement

'''
测试用例编号: DWH_CS_001
测试用例名称: 数据抽取、加工、集成功能测试用例-1
编写：林必涵
步骤1: 点击数据建模下的创建维表
步骤2: 点击选择表，选择一张源表，点击下一步
步骤3: 选择表的维度，点击下一步
步骤4: 选择维表的名称，描述，主题数据库，点击执行
预期结果: 维表创建成功，页面出现创建成功的提示
'''
class Test_DWH_CS_001:
    @pytest.fixture(scope='class', autouse=True)
    def setup_teardown(self, driver: WebDriver):
        self.setup()
        yield driver          # 这里可以返回对象给测试方法
        self.teardown()
        
        
    def setup(self):
        # 创建维表前置条件判断
        logging.info("测试用例 DWH_CS_001 开始执行")
        
    def teardown(self):
        # 删除创建的维表
        logging.info("测试用例 DWH_CS_001 执行结束")
        
    def test_case_001(self, driver: WebDriver):
        self.step1(driver)
        self.step2(driver)
        self.step3(driver)
        self.step4(driver)
        sleep(2)
        assert self.check(driver)
    
    def step1(self, driver: WebDriver):
        elements: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for element in elements:
            logging.info(f"菜单项文本：{element.accessible_name}")
            if element.accessible_name == "数据建模":
                element.click()
                break
        elements: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for element in elements:
            logging.info(f"菜单项文本：{element.accessible_name}")
            if element.accessible_name == "创建维表":
                element.click()
                break
            
        sleep(2)
        assert driver.current_url.endswith("#/dataModel/dim1"), "未跳转到创建维表页面"
    
    def step2(self, driver: WebDriver):
        # 填充选择表表单
        selectTableElm = htmlUtils.getElementByClassName(driver, 'el-select')
        selectTableElm.click()
        sleep(1)
        options: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for option in options:
            logging.info(f"选项文本：{option.accessible_name}")
            if option.accessible_name == "supplier":
                option.click()
                break
        sleep(2)
        buttons: list[WebElement] = htmlUtils.getElementsByXPath(driver, '//button[@type="button"]')
        for button in buttons:
            logging.info(f"按钮文本：{button.accessible_name}")
            if button.accessible_name == "下一步":
                button.click()
                break
        sleep(2)
        assert driver.current_url.endswith("#/dataModel/dim2"), "未跳转到选择维度页面"
        
        
    
    def step3(self, driver: WebDriver):
        selectTableElm = htmlUtils.getElementByClassName(driver, 'el-select')
        selectTableElm.click()
        sleep(1)
        options: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for option in options:
            logging.info(f"选项文本：{option.accessible_name}")
            if option.accessible_name == "s_city":
                option.click()
            if option.accessible_name == "s_name":
                option.click()
        sleep(1)
        selectTableElm.click()
        buttons: list[WebElement] = htmlUtils.getElementsByXPath(driver, '//button[@type="button"]')
        for button in buttons:
            logging.info(f"按钮文本：{button.accessible_name}")
            if button.accessible_name == "下一步":
                button.click()
                break
        sleep(2)
        assert driver.current_url.endswith("#/dataModel/dim3"), "未跳转到创建维表页面"

    def step4(self, driver: WebDriver):
        # 填写维表名
        inputElements: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'input')
        inputElements[0].send_keys("dwh_cs_001_dim")
        # 填写描述
        inputElements[1].send_keys("dwh_cs_001用例")
        sleep(1)
        
        # 选择主题数据库
        selectTableElm = htmlUtils.getElementByClassName(driver, 'el-select')
        selectTableElm.click()
        sleep(1)
        options: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for option in options:
            logging.info(f"选项文本：{option.accessible_name}")
            if option.accessible_name == "base1":
                option.click()
                break
        sleep(1)
        buttons: list[WebElement] = htmlUtils.getElementsByXPath(driver, '//button[@type="button"]')
        for button in buttons:
            logging.info(f"按钮文本：{button.accessible_name}")
            if button.accessible_name == "执行":
                button.click()
                break
        sleep(2)
    def check(self, driver: WebDriver) -> bool:
        elements: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for element in elements:
            logging.info(f"菜单项文本：{element.accessible_name}")
            if element.accessible_name == "数据资产":
                element.click()
                sleep(2)
                break
        elements: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'li')
        for element in elements:
            logging.info(f"菜单项文本：{element.accessible_name}")
            if element.accessible_name == "分层模型":
                element.click()
                sleep(2)
                break
        found = False
        while (not found):
            table_rows: list[WebElement] = htmlUtils.getElementsByTagName(driver, 'td')
            for row in table_rows:
                if row.accessible_name.find("dwh_cs_001_dim") != -1:
                    found = True
                    break
            if found:
                return True
            next_page_button: WebElement = htmlUtils.getElementByClassName(driver, 'btn-next')
            if next_page_button.is_enabled():
                next_page_button.click()
                sleep(2)
                continue
            return False
        
        
        
        
        
        
        